package io.github.talelin.latticy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.dto.omsware.OmsCheckDTO;
import io.github.talelin.latticy.dto.userware.UpdateUserwareDTO;
import io.github.talelin.latticy.model.OmsUserwareDO;
import io.github.talelin.latticy.mapper.OmsUserwareMapper;
import io.github.talelin.latticy.model.UserDO;
import io.github.talelin.latticy.service.OmsUserwareService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.latticy.service.feign.OpenFeignService;
import io.github.talelin.latticy.service.WarehouseService;
import io.github.talelin.latticy.vo.WarehouseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator@wulongbo
 * @since 2022-05-30
 */
@Service
public class OmsUserwareServiceImpl extends ServiceImpl<OmsUserwareMapper, OmsUserwareDO> implements OmsUserwareService {


    @Autowired
    private OpenFeignService openFeignService;

    @Autowired
    private WarehouseService warehouseService;

    @Override
    public List<WarehouseVO> getWareNameListByOmsUserId(Integer userId) {
        return this.baseMapper.getWareNameListByOmsUserId(userId);
    }

    @Override
    public List<WarehouseVO> getAllWarehouseByOmsRoot() {
        return warehouseService.getAllWarehouseByOmsRoot();
    }

    @Override
    public boolean checkWareExistByCode(OmsCheckDTO dto) {
        QueryWrapper<OmsUserwareDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(OmsUserwareDO::getCode, dto.getCode()).eq(OmsUserwareDO::getUserId, dto.getUserId());
        return this.count(wrapper) > 0;
    }

    @Transactional
    @Override
    public boolean updateOmsUserware(UpdateUserwareDTO dto) {
        Integer userId = dto.getUserId();
        List<String> codes = dto.getCodes();
        throwUserNotExistById(userId);
        if (CollectionUtils.isEmpty(codes)) {
            QueryWrapper<OmsUserwareDO> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(OmsUserwareDO::getUserId, userId);
            return this.baseMapper.delete(wrapper) > 0;
        }
        QueryWrapper<OmsUserwareDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(OmsUserwareDO::getUserId, userId);
        List<OmsUserwareDO> oldList = this.baseMapper.selectList(wrapper);
        List<String> oldCodes = oldList.parallelStream().map(OmsUserwareDO::getCode).collect(Collectors.toList());
        List<String> removeCodes = oldCodes.parallelStream().filter(code -> !codes.contains(code)).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(removeCodes)) {
            this.baseMapper.deleteRealByUserIdAndCodes(userId, removeCodes);
        }
        List<String> addCodes = codes.parallelStream().filter(code -> !oldCodes.contains(code)).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(addCodes)) {
            return true;
        }
        int count = warehouseService.getWarehouseCountByWareCodes(addCodes);
        if (count != addCodes.size()) {
            throw new NotFoundException(18303);
        }
        List<OmsUserwareDO> addList = addCodes.stream().map(code -> new OmsUserwareDO(userId, code)).collect(Collectors.toList());
        return this.saveBatch(addList);
    }

    private void throwUserNotExistById(Integer id) {
        UserDO omsUserDO = openFeignService.getOmsUserById(id);
        if (omsUserDO == null) {
            throw new NotFoundException(10021);
        }
    }
}
